package edu.unc.comp410.colemangeer.ui;

/**
 * Comp 410
 * Add area code entry to the GUI (no street address or phone number
 * required - we won't be validating these).
 * Add parameter validation to EnterHandler. Inform user of
 * mistakes and give her an opportunity to correct.
 */

import static java.lang.System.out;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import edu.unc.comp410.colemangeer.model.Customer;
import edu.unc.comp410.colemangeer.system.UIController;

//comment to demonstrate SVN commit/update/conflict
//hello again

public class NewCustomerEntryGUI  {
	
	private UIController controller = new UIController();
	
	
		// Text fields and their labels.
	private final String SPACING = "  ";
	private final JLabel CustomerIDLabel = new JLabel( "Customer ID Number (required)" + SPACING
		, SwingConstants.RIGHT );
	private final JTextField custIDfield = new JTextField();
	private final JLabel NameLabel = new JLabel( "Name (required)" + SPACING
		, SwingConstants.RIGHT );
	private final JTextField name = new JTextField( 25 );
	private final JLabel CityLabel = new JLabel( "City (required)" + SPACING
		, SwingConstants.RIGHT );
	private final JTextField city = new JTextField( 25 );
	
	private final JLabel StateLabel = new JLabel( "State (required)" + SPACING
			, SwingConstants.RIGHT );
		private final JTextField state = new JTextField( 25 );
	
	private final JLabel ZipcodeLabel = new JLabel( "Zipcode (required)" + SPACING
		, SwingConstants.RIGHT );
	private final JTextField zipcode = new JTextField();
	
	private final JLabel AreaCodeLabel = new JLabel( "Area Code (optional)" + SPACING, SwingConstants.RIGHT );
	private final JTextField areaCode = new JTextField(3);
	
		// Buttons and their event handlers.
	private final static JButton enterButton = new JButton( "Enter" );
	private final EnterHandler enterHandler = new EnterHandler();
	private final static JButton clearButton = new JButton( "Clear" );
	private final ClearHandler clearHandler = new ClearHandler();
	
	/**
	 * Define the action performed when "Clear" button is clicked.
	 * Note - anonymous inner class often used here.
	 */
	private class ClearHandler implements ActionListener {
		public void actionPerformed( ActionEvent e ) {
			custIDfield.setText( "" );
			name.setText( "" );
			city.setText( "" );
			zipcode.setText( "" );
			state.setText("");
			areaCode.setText("");
		}
	} // End ClearHandler class.
	
	/**
	 * Define the action performed when "Enter" button is clicked.
	 */
	private class EnterHandler implements ActionListener {
		public void actionPerformed( ActionEvent e ) {
			
			// Check each of the entries
			//   custIDfield.getText()
			//   city.getText()
			//   zipcode.getText()
			//   areacode.getText() - must be added to GUI
			
			Integer zipVal, custIdVal, areaCodeVal;
			
			try {
				custIdVal = Integer.parseInt(custIDfield.getText());

			} 
			catch (NumberFormatException e1) {
				custIdVal = null;
			}
			
			try {
				zipVal = Integer.parseInt(zipcode.getText());
			} catch (NumberFormatException e1) {
				zipVal = null;
			}
			try {
				areaCodeVal = Integer.parseInt(areaCode.getText());
			} catch (NumberFormatException e1) {
				areaCodeVal = null;
			}
			
			
			if (!Customer.STATEABBREV_CONSTRAINT.isLegal(state.getText())){
				JOptionPane.showMessageDialog(null, state.getText() + ": Bad state abbrev!");
			}
			//if ( zipVal==null || !Customer.ZIPCODE_CONSTRAINT.isLegal(zipVal)){
			if (!Customer.ZIPCODE_CONSTRAINT.isLegal(zipcode.getText())){
				JOptionPane.showMessageDialog(null, zipcode.getText() + ": Bad zip code!");}

			if(areaCode.getText().length() == 0){
			//there is nothing in area code, which is optional; no error	
			}
			else{
			
			// there is something in area code: it needs to be a number in range
			
			if ( areaCodeVal == null || !Customer.AREACODE_CONSTRAINT.isLegal(areaCodeVal)){
				JOptionPane.showMessageDialog(null, areaCode.getText() + ": Bad area code!");
			}
			}
			
			if (custIdVal==null || !Customer.CUSTID_CONSTRAINT.isLegal(custIdVal)){
				JOptionPane.showMessageDialog(null, custIDfield.getText() + ": Bad customer ID!");
			 }
		}
	} // End EnterHandler class.
	
	/**
	 * Create a panel that is the contents of the window except for
	 * its frame (window title plus buttons to minimize, maximize,
	 * and close the window).
	 */
	public Component createComponents() {

		JPanel pane = new JPanel( new GridLayout(8,2));

			// Place text fields.
		pane.add( CustomerIDLabel );
		pane.add( custIDfield );
		pane.add( NameLabel );
		pane.add( name );
		pane.add( CityLabel );
		pane.add( city );		
		pane.add( StateLabel );
		pane.add( state );		
		pane.add( ZipcodeLabel );
		pane.add( zipcode );
		pane.add( AreaCodeLabel );
		pane.add( areaCode );

			// Place buttons.
		pane.add( new JLabel( "" ) );
		enterButton.addActionListener( enterHandler );
		pane.add(enterButton);
		pane.add( new JLabel( "" ) );
		clearButton.addActionListener( clearHandler );
		pane.add( clearButton );
		
		pane.setBorder( BorderFactory.createEmptyBorder(10,10,10,10));
		return pane;
	}
	
	/**
	 * Creates a single frame with standard decorations
	 * (title + buttons to minimize, maximize, and close)
	 * and contents created by createComponents().
	 */
	private static void createAndShowGUI() {
		JFrame.setDefaultLookAndFeelDecorated( true );
		JFrame frame = new JFrame( "New Customer Entry");
		frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		frame.getRootPane().setDefaultButton( enterButton );
		NewCustomerEntryGUI app = new NewCustomerEntryGUI();
		Component contents = app.createComponents();
		frame.getContentPane().add( contents, BorderLayout.CENTER );
		frame.pack();
		frame.setVisible( true );
	}
	
	/**
	 * Thread-safe execution of this GUI. You don't have to understand
	 * this. Just us it as-is for all your Swing programs.
	 */
	public static void main( String[] args ) {
		System.out.println( "Main called" );
		javax.swing.SwingUtilities.invokeLater( new Runnable() {
			public void run() { createAndShowGUI(); }
		});
		System.out.println( "Main finished" );
	}

}
